package wang.aweb.childcar.controller.agentApi;

import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import wang.aweb.childcar.common.ApiRestResponse;
import wang.aweb.childcar.filter.AgentFilter;
import wang.aweb.childcar.model.pojo.WqPage;
import wang.aweb.childcar.model.query.CzOrderParam;
import wang.aweb.childcar.model.vo.MemberVO;
import wang.aweb.childcar.service.CzOrderService;
import wang.aweb.childcar.service.MemberService;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

@RestController
@RequestMapping("/agentApi/czOrder/")
public class AgentCzOrderController {
    @Autowired
    private MemberService memberService;
    @Autowired
    private CzOrderService czOrderService;
    @PostMapping("getList")
    public ApiRestResponse getList(@RequestBody CzOrderParam czOrderParam){
        if(!StringUtils.isEmpty(czOrderParam.getDate())){
            SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            String startstr=czOrderParam.getDate()+" 00:00:00";
            String endstr=czOrderParam.getDate()+" 23:59:59";
            try {
                Date starttime=sdf.parse(startstr);
                czOrderParam.setStarttime(starttime);
                Date endtime=sdf.parse(endstr);
                czOrderParam.setEndtime(endtime);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        czOrderParam.setMoney(czOrderParam.getMoney()*100);
        czOrderParam.setAgentId(AgentFilter.agentThreadLocal.get().getId());
        if(!StringUtils.isEmpty(czOrderParam.getMobile())){
            MemberVO currentMember=memberService.selectByMoblile(czOrderParam.getMobile());
            if(currentMember!=null){
                czOrderParam.setMemberId(currentMember.getId());
            }else{
                czOrderParam.setMemberId(0);
            }
        }
        PageInfo pageInfo=czOrderService.getList(czOrderParam.getPage(),czOrderParam.getLimit(),czOrderParam);
        WqPage wqPage=new WqPage(pageInfo.getTotal(),pageInfo.getList());
        return ApiRestResponse.success(wqPage);
    }
}
